1765D - Watch the Videos - CodeForces Solution


binary search constructive algorithms two pointers *1700

Please click on ads to support us..

Python Code:

n, m = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
a.sort()
s = sum(a)
def possible(b, m):
    l = len(b)
    for i in range(l//2):
        if b[i]+b[l-1-i] > m:
            return False
    return True
minp = 1
maxp = n
while minp != maxp:
    midp = (minp+maxp+1)//2
    if possible(a[:midp], m):
        minp = midp
    else: maxp = midp-1
print(sum(a)+n+1-minp)

    

C++ Code:

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <ctype.h>
#include <queue>
#include <cstring>
#include <set>
#include <bitset>
#include <map>
#include <chrono>
#include <random>
#include <unordered_map>
#include <stdio.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef std::vector<int> vi;
typedef std::vector<bool> vb;
typedef std::vector<string> vs;
typedef std::vector<double> vd;
typedef std::vector<long long> vll;
typedef std::vector<std::vector<int> > vvi;
typedef vector<vll> vvll;
typedef std::vector<std::pair<int, int> > vpi;
typedef vector<vpi> vvpi;
typedef std::pair<int, int> pi;
typedef std::pair<ll, ll> pll;
typedef std::vector<pll> vpll;

const long long mod = 1000000007;
ll gcd (ll a, ll b) {return b==0 ? a : gcd(b, a%b);}
const unsigned gen_seed = std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937_64 gen(gen_seed);

#define all(c) (c).begin(),(c).end()
#define srt(c) sort(all(c))
#define srtrev(c) sort(all(c)); reverse(all(c))
#define forn(i, a, b) for(int i = a; i < b; i++)
#define read(x) scanf("%d", &x)
#define readv(x, n) vi x(n); forn(i,0,n) scanf("%d", &x[i])

#define pb push_back
#define mp make_pair
vi a ;
int64_t n , m ;
int64_t can ( int x ) {
      int l = x-1, r =n-1 ;
      while (l<r){
         if ( a[l]+a[r] >m){return 0 ; }
         else { l++, r--; }
      }
      if ( l==r ) {
        if ( a[l]+a[r+1]>m){return 0 ;}
        else {l++; r--; }
      }
      return 1;
}

int main()
{
#ifdef LOCAL
    freopen("input.txt", "rt", stdin);
    freopen("output.txt", "wt", stdout);
#endif
    int ta;
    ta = 1 ;


    forn(ifa,0,ta) {

  cin>>n>>m ;

         a= vi(n) ;
         int64_t sum =0  ;
         forn ( i,0, n ) {cin >>a[i] ;sum+=a[i] ;}

      srtrev(a);
      int l= 1 , r = n;

      while (r-l>1){
          int mid = (l+r)/2;
          if (can ( mid ) ) r = mid ;
          else l = mid ;

      }

      if ( can(l )){ r = l ; }
      cout<<sum + r <<endl;

    }


}


Comments

Submit
0 Comments
More Questions

2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets
1630A - And Matching
1630B - Range and Partition
1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter
151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff